package com.yungu.swift.common.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.common.dao.CommonTagMapper;
import com.yungu.swift.common.model.dto.CommonStarTagDto;
import com.yungu.swift.common.model.dto.CommonTagDto;
import com.yungu.swift.common.model.dto.CommonTextTagDto;
import com.yungu.swift.common.model.param.AdminTagParam;
import com.yungu.swift.common.service.CommonTagService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * CommonTagServiceImpl
 **/

@Service
public class CommonTagServiceImpl extends BaseServiceImpl<CommonTagDto> implements CommonTagService {

    @Autowired
    private CommonTagMapper commonTagMapper;

    @Override
    protected IMapper<CommonTagDto> getMapper() {
        return commonTagMapper;
    }

    @Override
    public ResponseData<List<CommonStarTagDto>> listStarTag(Integer tagType, Integer clientSide) {
        return ResponseData.buildSuccessResponse(commonTagMapper.listStarTag(
                MapUtils.build(new String[]{"tagType", "clientSide"}, new Object[]{1, clientSide})));
    }

    @Override
    public ResponseData<List<CommonTextTagDto>> listTextTag(Integer tagType, Integer clientSide) {
        return ResponseData.buildSuccessResponse(commonTagMapper.listTextTag(
                MapUtils.build(new String[]{"tagType", "clientSide"}, new Object[]{tagType, clientSide})));
    }

    @Override
    public ResponseData<Boolean> verification(String tagCode, String tagName, String tagNameEn) {
        Map<String, Object> objectMap = MapUtils.build();
        objectMap.put("tagCode", tagCode);
        objectMap.put("tagName", tagName);
        objectMap.put("tagNameEn", tagNameEn);
        return ResponseData.buildSuccessResponse(commonTagMapper.verification(objectMap) == 0);
    }

    @Override
    public ResponseData<Boolean> saveOrUpdate(AdminTagParam tagParam) {
        CommonTagDto commonTagDto = new CommonTagDto();
        BeanUtils.copyProperties(tagParam, commonTagDto);
        if (tagParam.getUuid() == null) {
            commonTagDto.setUuid(StringUtils.buildUUID());
            //新增默认开启
            commonTagDto.setTagStatus(CommonConstant.STATUS_OPENED);
            commonTagDto.setCreateBy(tagParam.getUserUuid());
            commonTagDto.setCreateOn(new Date());
            commonTagDto.setAppid(tagParam.getAppid());
            //用户新增的 暂时默认用户级
            commonTagDto.setTagProperty(2);
            return add(commonTagDto);
        }
        commonTagDto.setUpdateBy(tagParam.getUserUuid());
        commonTagDto.setUpdateOn(new Date());
        return edit(commonTagDto);
    }
}